package com.aabte.lota.auth.auth.shiro.configure;

import com.aabte.lota.auth.auth.shiro.JwtTokenCredentialsMatcher;
import com.aabte.lota.auth.auth.shiro.JwtTokenRealm;
import com.aabte.lota.auth.auth.shiro.UsernamePasswordCredentialsMatcher;
import com.aabte.lota.auth.auth.shiro.UsernamePasswordRealm;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Daniel
 * @version 1.0
 * @date 2020/4/8
 */
@Configuration
@ConditionalOnProperty(name = "shiro.enabled", matchIfMissing = true)
public class ShiroRealmConfig {

  public static final int HASH_ITERATIONS = 15;

  /** 自定义Realm */
  @Bean(name = "usernamePasswordRealm")
  public UsernamePasswordRealm usernamePasswordRealm() {
    UsernamePasswordRealm realm = new UsernamePasswordRealm();
    realm.setCredentialsMatcher(usernamePasswordCredentialsMatcher());
    realm.setCachingEnabled(false);
    return realm;
  }

  /** 自定义Realm */
  @Bean(name = "jwtTokenRealm")
  public JwtTokenRealm jwtTokenRealm() {
    JwtTokenRealm realm = new JwtTokenRealm();
    realm.setCredentialsMatcher(new JwtTokenCredentialsMatcher());
    realm.setCachingEnabled(false);
    return realm;
  }

  /** 凭证匹配器 */
  @Bean
  public UsernamePasswordCredentialsMatcher usernamePasswordCredentialsMatcher() {
    UsernamePasswordCredentialsMatcher credentialsMatcher =
        new UsernamePasswordCredentialsMatcher();
    credentialsMatcher.setHashIterations(HASH_ITERATIONS);
    return credentialsMatcher;
  }
}
